package com.community.process._1010.controller;


import com.community.process._1010.common.api.CommonResult;
import com.community.process._1010.common.utils.CommonUtils;
import com.community.process._1010.dao.entity.ProcessOrder;
import com.community.process._1010.dao.entity.ProcessTOrder;
import com.community.process._1010.dao.query.ProcessOrderQuery;
import com.community.process._1010.dao.query.ProcessTOrderQuery;
import com.community.process._1010.dao.view.ProcessOrderView;
import com.community.process._1010.dao.view.ProcessTOrderView;
import com.community.process._1010.domains.ProcessOrderDTO;
import com.community.process._1010.service.ProcessOrderService;
import com.oneadds.lowcode.dto.PageListData;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 到餐加工子订单 前端控制器
 *
 * @author Zbs
 * @since 2024-09-05
 */
@RestController
@Tag(name = "到餐加工订单")
@RequestMapping("/process/order")
@RequiredArgsConstructor
public class ProcessOrderController {

    private final ProcessOrderService processOrderService;

    @PostMapping("/cartCreateOrder")
    @Operation(summary = "购物车创建订单")
    public CommonResult<ProcessTOrderView> cartCreateOrder(@RequestBody @Valid ProcessOrderDTO.CartCreateOrder dto, HttpServletRequest request) {
        ProcessTOrderView order = this.processOrderService.createOrder(dto, CommonUtils.getLoginId(), request);
        return CommonResult.success(order);
    }

    @PostMapping("/cartMiniCreateOrder")
    @Operation(summary = "购物车创建订单")
    public CommonResult<ProcessTOrderView> cartMiniCreateOrder(@RequestBody @Valid ProcessOrderDTO.CartCreateOrder dto, HttpServletRequest request) {
        ProcessTOrderView order = this.processOrderService.cartMinieOrder(dto, CommonUtils.getLoginId(), request);
        return CommonResult.success(order);
    }


    @PostMapping("/queryTOrder")
    @Operation(summary = "查询主订单列表")
    public CommonResult<PageListData<ProcessTOrderView>> queryTOrder(@RequestBody ProcessTOrderQuery queryVo) {
        return CommonResult.success(this.processOrderService.queryTOrder(queryVo,null));
    }

    @PostMapping("/queryMyTOrder")
    @Operation(summary = "我的主订单列表")
    public CommonResult<PageListData<ProcessTOrderView>> queryMyTOrder(@RequestBody ProcessTOrderQuery queryVo) {
        return CommonResult.success(this.processOrderService.queryTOrder(queryVo,CommonUtils.getLoginId()));
    }

    @PostMapping("/queryCOrder")
    @Operation(summary = "查询子订单列表")
    public CommonResult<PageListData<ProcessOrderView>> queryCOrder(@RequestBody ProcessOrderQuery queryVo) {
        return CommonResult.success(this.processOrderService.queryCOrder(queryVo,null));
    }

    @PostMapping("/queryMyCOrder")
    @Operation(summary = "我的子订单列表")
    public CommonResult<PageListData<ProcessOrderView>> queryMyCOrder(@RequestBody ProcessOrderQuery queryVo) {
        return CommonResult.success(this.processOrderService.queryCOrder(queryVo,CommonUtils.getLoginId()));
    }

    @PostMapping("/queryTOrder/detail/{id}")
    @Operation(summary = "主订单详情查询")
    public CommonResult<ProcessTOrderView> queryTOrderDetail(@PathVariable Long id) {
        return CommonResult.success(this.processOrderService.queryTOrderDetail(id));
    }

    @PostMapping("/queryOrder/detail/{id}")
    @Operation(summary = "订单详情查询")
    public CommonResult<ProcessOrderView> queryCOrderDetail(@PathVariable Long id) {
        return CommonResult.success(this.processOrderService.queryCOrderDetail(id));
    }

    @PostMapping("/cancelAnOrder/{orderId}")
    @Operation(summary = "取消订单")
    public CommonResult<String> cancelAnOrder(@PathVariable Long orderId,HttpServletRequest request) {
        this.processOrderService.cancelAnOrder(orderId, CommonUtils.getLoginId(),request);
        return CommonResult.success("");
    }

    @PostMapping("/applyForARefund")
    @Operation(summary = "申请退款")
    public CommonResult<String> applyForARefund(@RequestBody ProcessOrderDTO.ApplyForARefundDTO dto) {
        this.processOrderService.applyForARefund(dto, CommonUtils.getLoginId());
        return CommonResult.success("");
    }

    @PostMapping("/refusedToRefund")
    @Operation(summary = "拒绝退款")
    public CommonResult<String> refusedToRefund(@RequestBody ProcessOrderDTO.RefundForARefundDTO dto) {
        this.processOrderService.refusedToRefund(dto, CommonUtils.getLoginId());
        return CommonResult.success("");
    }

    @PostMapping("/agreeToRefund/{orderId}")
    @Operation(summary = "同意退款")
    public CommonResult<String> agreeToRefund(@PathVariable Long orderId,HttpServletRequest request) {
        this.processOrderService.agreeToRefund(orderId, CommonUtils.getLoginId(),request);
        return CommonResult.success("");
    }

}

